<?php
namespace StarCloud\Models;

trait TaskExportTrait
{
    protected $canBeEmptyStringColumns = [
        'oss_url',
        'task_params',
        'note',
    ];

    public static function newTask($taskFlag, $taskName, array $params = [], $uid = 0)
    {
        $task = new self();
        $task->task_name = $taskName;
        $task->task_flag = $taskFlag;
        $task->uid = $uid;
        $task->task_params = json_encode($params);
        $task->oss_url = '';
        $task->status = self::STATUS_DEFAULT;
        $task->note = '';

        $task->save();

        return $task;
    }

    public function setExportSuccess($ossUrl)
    {
        $this->status = self::STATUS_SUCCESS;
        $this->oss_url = $ossUrl;
        $this->note = 'success';
        $this->save();
    }

    public function setExportIng()
    {
        $this->status = self::STATUS_EXPORT_ING;
        $this->note = 'exporting';
        $this->save();
    }

    public function setExportFail($note)
    {
        $this->status = self::STATUS_FAILED;
        $this->note = $note;
        $this->save();
    }

    public function getTaskParams()
    {
        return json_decode($this->task_params, true);
    }

    protected $id;
    protected $task_name;
    protected $task_flag;
    protected $uid;
    protected $task_params;
    protected $oss_url;
    protected $status;
    protected $note;
    protected $created_at;
    protected $updated_at;
}
